<template>
   <div>
        <el-dialog :visible.sync="editerData.show" :fullscreen="fullscreen" :width="width" center :close-on-click-modal='false' @open='openEditer'>
            <el-radio-group style='margin-top:-50px' v-model="editerData.editerBody" size='small'>
                <el-radio-button label="商家信息"></el-radio-button>
                <el-radio-button label="服务配置" :disabled="editerData.merchant.merchantId==''"></el-radio-button>
                <el-radio-button label="小程序配置" :disabled="editerData.merchant.merchantId==''"></el-radio-button>
            </el-radio-group>
            <div style='margin-top:30px;height:400px' v-loading='editerData.editerLoading'>

                <div style='width:800px;margin:auto'>
                    <merchantEditer ref="merchantEditer" :merchant='editerData.merchant' v-show="editerData.editerBody=='商家信息'"></merchantEditer>
                    <wechatMerchantEditer :wechatMerchant='editerData.wechatMerchant' v-show="editerData.editerBody=='小程序配置'"></wechatMerchantEditer>
                </div>
                
                <div style="width:1250px" v-show="editerData.editerBody=='服务配置'">
                    <div style="float:left;width:400px;height:400px">
                        <businessLicenceImage :merchant='editerData.merchant'></businessLicenceImage>
                    </div>
                    <div style="float:left;width:600px">
                        <functionPackageEditer :merchant='editerData.merchant' :renew='editerData.renew'></functionPackageEditer>
                    </div>
                    <div style="float:left;width:250px;height:400px;overflow-y:auto">
                        <merchantCatEditer :catIds='editerData.catIds' :cateTree="editerData.cateTree"></merchantCatEditer>
                    </div>
                </div>
            </div>
            <span slot="footer">
              <el-button @click="editerData.show=false">取消</el-button>
              <el-button type="primary" @click="onsubmit">确定</el-button>
            </span>
        </el-dialog>
   </div>
</template>

<script>
import merchantEditer from "@/components/merchant/merchantEditer"
import functionPackageEditer from "@/components/merchant/functionPackageEditer"
import wechatMerchantEditer from "@/components/merchant/wechatMerchantEditer"
import merchantCatEditer from "@/components/merchant/merchantCatEditer"
import businessLicenceImage from "@/components/merchant/businessLicenceImage"

export default {
    name: 'editer',
    data() {
      return {
          fullscreen:false,
          width:"1300px"
      };
    },
    methods: {
        onsubmit:function(){
            if(this.editerData.editerLoading)return;
            this.editerData.editerLoading=true;
            this.$refs.merchantEditer.$refs.merchantForm.validate(
                valid=> {
                    if(valid)this.submitExecute();
                    else{
                        this.editerData.editerLoading=false;
                        if(this.editerData.editerBody!="商家信息"){
                            this.editerData.editerBody="商家信息";
                            this.$refs.merchantEditer.$refs.merchantForm.validate(valid=>{});
                        }
                    }
                }
            );
        },
       submitExecute:function(){
            var params = new URLSearchParams();
            this.editerData.merchant.merchantName=this.editerData.merchant.companyName;
            for(var key in this.editerData.merchant){
                params.append(key,this.editerData.merchant[key]);
            }
            for(var key in this.editerData.wechatMerchant){
                if(key!="merchantId")params.append(key,this.editerData.wechatMerchant[key]);
            }
            for(var i=0;i<this.editerData.catIds.length;i++){
                params.append('catIds',this.editerData.catIds[i]);
            }
            params.append('renewal',this.editerData.renew.renewal);
            params.append('cost',this.editerData.renew.cost);
            this.$http.post(this.$store.state.url+"/merchant/save",params).then(
                response=>{
                    if(response.data.status==200){
                        this.$emit("edited",response.data.data);
                        this.$message({message: '保存成功',type: 'success'});
                    }else{
                        this.$message.error('保存失败,'+response.data.message);
                    }
                    this.editerData.editerLoading=false;
                }
            ).catch(
                error=>{
                    this.$message.error('保存失败');
                    this.editerData.editerLoading=false;
                }
            );
        },
        //商家编辑器的启动过程
        openEditer:function(){
            if(this.$refs.merchantEditer!=null)this.$refs.merchantEditer.$refs.merchantForm.resetFields();
            if(this.editerData.merchant.merchantId==''){
                this.editerInit();
                this.editerData.editerLoading=false;
            }else{
                this.editerData.renew={renewal:0,cost:0.00};
                var params=new URLSearchParams();
                params.append('merchantId',this.editerData.merchant.merchantId);
                this.$http.post(this.$store.state.url+"/merchant/info",params).then(
                    response=>{
                        if(response.data.status==200){
                            this.editerData.merchant=response.data.data.merchant;
                            this.editerData.merchant.imageList=[];
                            if(this.editerData.merchant.businessLicence!=''){
                                var temp=this.editerData.merchant.businessLicence.split(',');
                                for(var i=0;i<temp.length;i++){
                                    this.editerData.merchant.imageList.push({name:temp[i],url:temp[i]});
                                }
                            }
                            this.editerData.wechatMerchant=response.data.data.wechatMerchant;
                            this.editerData.catIds=response.data.data.catIds;
                            this.catTreeInit();
                            this.editerData.editerLoading=false;
                        }else{
                            this.$message.error(response.data.message);
                            this.editerData.show=false;
                        }
                    }
                ).catch(
                    error=>{
                        this.$message.error("编辑器打开失败");
                        this.editerData.show=false;
                    }
                );
            }
        },
        //新建商家时编辑器的初始化
        editerInit:function(){
            this.editerData.merchant={
                merchantId:'',
                merchantCode:'',
                merchantName:'',
                loginName:'',
                merchantType:'PERSON',
                companyName:'',
                companyPhone:'',
                address:'',
                contactName:'',
                contactMobile:'',
                wechat:'',
                email:'',
                qq:'',
                contract:'',
                businessLicence:'',
                imageList:[],
                activationFee:0.00,
                miniAppsEntrance:false,
                h5Entrance:false,
                singleClassify:false,
                catId:'',
                moreClassify:false,
                basicMall:false,
                seniorMall:false,
                vinSearch:false,
                vinNum:0,
                qixiubaoStyle:false,
                cascader:false,
                memberAudit:true,
                mustLogin:true,
                limitMonth:null,
                remark:''
            };
            this.editerData.wechatMerchant={
                wechatMerchantId:'',
                merchantId:'',
                wechatmerchantName:'',
                wechatmerchantCode:'',
                pubAppId:'',
                pubAppSecret:'',
                appId:'',
                secret:'',
                apiKey:''
            }
            this.editerData.catIds=[];
            this.editerData.cateTree=[];
            this.editerData.renew={renewal:0,cost:0.00};
        },
        //获取全局的商品分类树
        catTreeInit:function(){
            var params=new URLSearchParams();
            params.append('token','1');
            params.append('userId','1');
            this.$http.post(this.$store.state.url+"/category/querytree",params).then(
                response=>{
                    if(response.data.status==200)this.editerData.cateTree=response.data.data.children;
                }
            ).catch(
                error=>{

                }
            );
        },
    },
    components:{
        merchantEditer:merchantEditer,
        functionPackageEditer:functionPackageEditer,
        wechatMerchantEditer:wechatMerchantEditer,
        merchantCatEditer:merchantCatEditer,
        businessLicenceImage:businessLicenceImage
    },
    created:function(){
        var x=window.innerWidth;
        if(x<1440){
            this.fullscreen=true,
            this.width="100%";
        }
        
    },
    props:['editerData']
}

</script>

<style scoped>

</style>
